<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>共享出行 - 个人中心</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        .header {
            background-color: #007bff;
            color: white;
            padding: 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            margin: 0;
            font-size: 1.5rem;
        }
        .main-content {
            padding: 2rem;
            max-width: 1200px;
            margin: 0 auto;
        }
        .profile-section {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 1.5rem;
            margin-bottom: 2rem;
        }
        .profile-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 1.5rem;
        }
        .info-card {
            padding: 1rem;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .info-card h3 {
            margin-top: 0;
            color: #333;
        }
        .info-card p {
            margin: 0.5rem 0;
            color: #666;
        }
        .trip-history {
            margin-top: 2rem;
        }
        .trip-list {
            display: grid;
            gap: 1rem;
        }
        .trip-card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 1rem;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
            align-items: center;
        }
        .trip-info p {
            margin: 0.5rem 0;
            color: #666;
        }
        .trip-info strong {
            color: #333;
        }
        .button {
            padding: 0.75rem 1.5rem;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .payment-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }
        .modal-content {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            max-width: 500px;
            margin: 2rem auto;
        }
        .form-group {
            margin-bottom: 1rem;
        }
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #666;
        }
        .form-group input {
            width: 100%;
            padding: 0.5rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .close {
            float: right;
            cursor: pointer;
            font-size: 1.5rem;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>共享出行系统</h1>
        <button class="button" onclick="logout()">退出登录</button>
    </div>

    <div class="main-content">
        <div class="profile-section">
            <h2>个人信息</h2>
            <div class="profile-info" id="profileInfo">
                <!-- 用户信息将通过JavaScript动态生成 -->
            </div>
        </div>

        <div class="profile-section">
            <h2>行程历史</h2>
            <div class="trip-history">
                <div class="trip-list" id="tripList">
                    <!-- 行程列表将通过JavaScript动态生成 -->
                </div>
            </div>
        </div>
    </div>

    <!-- 支付模态框 -->
    <div class="payment-modal" id="paymentModal">
        <div class="modal-content">
            <span class="close" onclick="closePaymentModal()">&times;</span>
            <h2>支付订单</h2>
            <div class="form-group">
                <label for="cardNumber">银行卡号</label>
                <input type="text" id="cardNumber" maxlength="19" placeholder="请输入银行卡号">
            </div>
            <div class="form-group">
                <label for="expiryDate">有效期</label>
                <input type="text" id="expiryDate" maxlength="5" placeholder="MM/YY">
            </div>
            <div class="form-group">
                <label for="cvv">CVV</label>
                <input type="text" id="cvv" maxlength="3" placeholder="请输入CVV">
            </div>
            <button class="button" onclick="processPayment()">确认支付</button>
        </div>
    </div>

    <script>
        let currentUser = null;
        let currentTrip = null;

        // 检查登录状态
        async function checkLogin() {
            try {
                const response = await fetch('api/check-login');
                const result = await response.json();
                
                if (!result.success) {
                    window.location.href = 'login.html';
                    return;
                }
                
                currentUser = result.data;
                loadProfile();
                loadTripHistory();
            } catch (error) {
                console.error('检查登录状态失败:', error);
                window.location.href = 'login.html';
            }
        }

        // 加载用户信息
        function loadProfile() {
            const profileInfo = document.getElementById('profileInfo');
            profileInfo.innerHTML = `
                <div class="info-card">
                    <h3>基本信息</h3>
                    <p><strong>用户名：</strong>${currentUser.username}</p>
                    <p><strong>手机号：</strong>${currentUser.phone}</p>
                    <p><strong>注册时间：</strong>${new Date(currentUser.createTime).toLocaleString()}</p>
                </div>
                <div class="info-card">
                    <h3>账户信息</h3>
                    <p><strong>账户余额：</strong>¥${currentUser.balance}</p>
                    <p><strong>信用评分：</strong>${currentUser.creditScore}</p>
                </div>
            `;
        }

        // 加载行程历史
        async function loadTripHistory() {
            try {
                const response = await fetch('api/trips/history');
                const result = await response.json();
                
                if (result.success) {
                    const tripList = document.getElementById('tripList');
                    tripList.innerHTML = result.data.map(trip => `
                        <div class="trip-card">
                            <div class="trip-info">
                                <p><strong>车辆类型：</strong>${trip.vehicleType}</p>
                                <p><strong>车牌号：</strong>${trip.plateNumber}</p>
                                <p><strong>开始时间：</strong>${new Date(trip.startTime).toLocaleString()}</p>
                                <p><strong>结束时间：</strong>${trip.endTime ? new Date(trip.endTime).toLocaleString() : '进行中'}</p>
                                <p><strong>费用：</strong>¥${trip.cost || '未结算'}</p>
                                <p><strong>状态：</strong>${getStatusText(trip.status)}</p>
                            </div>
                            <div class="trip-actions">
                                ${trip.status === 2 && !trip.paid ? `
                                    <button class="button" onclick="showPaymentModal(${trip.id}, ${trip.cost})">支付</button>
                                ` : ''}
                                ${trip.status === 2 && !trip.rating ? `
                                    <button class="button" onclick="goToRating(${trip.id})">评价</button>
                                ` : ''}
                            </div>
                        </div>
                    `).join('');
                }
            } catch (error) {
                console.error('加载行程历史失败:', error);
            }
        }

        // 获取状态文本
        function getStatusText(status) {
            switch (status) {
                case 1: return '进行中';
                case 2: return '已完成';
                case 3: return '已取消';
                default: return '未知状态';
            }
        }

        // 显示支付模态框
        function showPaymentModal(tripId, amount) {
            currentTrip = { id: tripId, amount };
            document.getElementById('paymentModal').style.display = 'block';
        }

        // 关闭支付模态框
        function closePaymentModal() {
            document.getElementById('paymentModal').style.display = 'none';
            currentTrip = null;
        }

        // 处理支付
        async function processPayment() {
            const cardNumber = document.getElementById('cardNumber').value;
            const expiryDate = document.getElementById('expiryDate').value;
            const cvv = document.getElementById('cvv').value;

            if (!cardNumber || !expiryDate || !cvv) {
                alert('请填写完整的支付信息');
                return;
            }

            try {
                const response = await fetch('api/payment', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        tripId: currentTrip.id,
                        amount: currentTrip.amount,
                        cardNumber,
                        expiryDate,
                        cvv
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    alert('支付成功');
                    closePaymentModal();
                    loadProfile();
                    loadTripHistory();
                } else {
                    alert(result.message);
                }
            } catch (error) {
                console.error('支付失败:', error);
                alert('支付失败，请稍后重试');
            }
        }

        // 跳转到评价页面
        function goToRating(tripId) {
            window.location.href = `trip.html?id=${tripId}`;
        }

        // 退出登录
        async function logout() {
            try {
                const response = await fetch('api/logout', {
                    method: 'POST'
                });
                
                const result = await response.json();
                
                if (result.success) {
                    window.location.href = 'login.html';
                }
            } catch (error) {
                console.error('退出登录失败:', error);
            }
        }

        // 页面加载时执行
        window.onload = checkLogin;
    </script>
</body>
</html> 